package com.zhkj.txg.module.home.adapter

import android.graphics.Paint
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseNodeAdapter
import com.chad.library.adapter.base.entity.node.BaseNode
import com.chad.library.adapter.base.provider.BaseNodeProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zhkj.lib.utils.AccountManager
import com.zhkj.lib.utils.SpanUtil
import com.zhkj.txg.R
import com.zhkj.txg.module.home.entity.MarketCateListEntity
import com.zhkj.txg.module.home.entity.SellEntity
import com.zhkj.txg.module.home.ui.ProductListActivity
import com.zhkj.txg.module.product.ui.ProductDetailActivity
import com.zhkj.txg.utils.ImageUtils

class SecondProductListAdapter : BaseNodeAdapter() {

    init {
        addFullSpanNodeProvider(HeaderItemProvider())
        addNodeProvider(SecondProductProvider())
    }

    override fun getItemType(data: List<BaseNode>, position: Int): Int {
        return when (data[position]) {
            is SellEntity -> 1
            else -> 0
        }
    }
}

class HeaderItemProvider : BaseNodeProvider() {
    override val itemViewType: Int = 0
    override val layoutId: Int = R.layout.item_second_header

    init {
        addChildClickViewIds(R.id.tvMarketCategoryMore)
    }

    override fun convert(helper: BaseViewHolder, data: BaseNode) {
        (data as? MarketCateListEntity)?.let {
            helper.setText(R.id.tvMarketCategoryTitle, it.name)
        }
    }

    override fun onChildClick(helper: BaseViewHolder, view: View, data: BaseNode, position: Int) {
        if (view.id == R.id.tvMarketCategoryMore) (data as? MarketCateListEntity)?.let {
            ProductListActivity.categoryOpen(context, it.name, it.id)
        }
    }
}

class SecondProductProvider : BaseNodeProvider() {
    override val itemViewType: Int = 1
    override val layoutId: Int = R.layout.item_home_goods

    override fun convert(helper: BaseViewHolder, data: BaseNode) {
        (data as? SellEntity)?.let {
            val imageView = helper.setText(R.id.tvGoodsTitle, it.goodName)
                .setText(R.id.tvGoodsPrice, SpanUtil.setMoneySymbol(it.price, true))
                .setVisible(R.id.tvGoodsTag, TextUtils.equals(it.isNew, "1"))
                .getView<ImageView>(R.id.ivGoodsThumb)
            ImageUtils.loadImage(context, imageView, it.image)
            // 设置是显示市场价还是显示分享赚多少钱
            with(helper.getView<TextView>(R.id.tvSellSpread)) {
                if (AccountManager.isMember() && it.shareEarn.isNotEmpty() && it.shareEarn != "0") {
                    setTextColor(ContextCompat.getColor(context, R.color.colorMain))
                    paint.flags = Paint.ANTI_ALIAS_FLAG
                    text = SpanUtil.setPrefixMoneyStr(price = it.shareEarn)
                } else {
                    setTextColor(ContextCompat.getColor(context, R.color.colorLightText))
                    paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
                    paint.isAntiAlias = true
                    text = SpanUtil.setMoneySymbol(it.marketPrice)
                }
            }
        }
    }

    override fun onClick(helper: BaseViewHolder, view: View, data: BaseNode, position: Int) {
        (data as? SellEntity)?.let {
            ProductDetailActivity.normalProduct(context, it.id)
        }
    }
}